# -*- coding: utf-8 -*-


# cmap2owl -- Helper application to convert from concept maps to OWL ontologies
# Copyright (c) 2008-2013  Rodrigo Rizzi Starr
#  
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#  
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#  
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


'''
Contains some simple inference rules about concepts

@author: Rodrigo Rizzi Starr
@copyright: Copyright © 2008-2013 Rodrigo Rizzi Starr
@license: MIT License
@contact: rodrigo.starr@gmail.com
'''


from mapperRule import MapperRule
from mapperQuestion import MapperQuestion
from infoProvider import SimpleInfoProvider
from actionProvider import ActionProvider
from log import log
log.debug("Loaded...")


class FunctionalRule(MapperRule):
    "Infers the existence of a functional relation"

    def __init__(self, db):
        
        classifiers = [
                       "functionalRelationPhrases",
                         ]
          
        # Heuristics to identify disjoint classes
        rules = [
                 "inferenceGroup(functionalRule, [functional(_)])",
                 
                 "functional(Relation) :-"
                    "positive(relation(Relation)),"
                    "label(Relation, Label),"
                    "functionalRelationPhrases(Label)",
                    
                 "functionalOWL(Relation) :- isTrue(functional(Relation))",
                 "isTrue(relation(Relation)) :- isTrue(functional(Relation))",
                 "isTrue(class(Concept)) :- isTrue(functional(Relation)),"
                    "fromPartRelation(Relation, Concept)",
                 "isTrue(class(Concept)) :- isTrue(functional(Relation)),"
                    "toPartRelation(Relation, Concept)",
                 
                    ]
         
        MapperRule.__init__(self, "FunctionalRules", db, rules, classifiers)
          
    def getQuestions(self):
        "Questions about functional relations"
        
        questions = []
        query = "possible(functional(Relation))"
                
        concepts = self.db.query(query)
        
        for answer in concepts:
            answer["Ids"] = [answer["Relation"]]
            questions.append(MapperQuestion(
                self.db,
                answer,
                'Does it make sense to say that "<b>%(Relation)s</b>" can have '
                    'one and only one value ?',
                "functional(%(Relation)s)",
                 helpFile = "functional",
                ))
              
        return questions
  
    def getInfoProvider(self):
        "Returns an info provider about functional relations"
        
        return SimpleInfoProvider(self.db,
                            "isTrue(functional(%s))",
                            "This relation has one and only one value")
        
    def getActionsProvider(self):
        """Returns actions providers for this rule"""
        
        return ActionProvider(self.db,
                            "equivalentLinkingPhrase(Element, %s),"
                                "label(Element, Label),"
                                "\+ functionalRelationPhrases(Label)",
                            "Does this phrase "
                                "indicate a functional relation ?",
                            "functionalRelationPhrases(%(Label)s)")
        
